package com.guguskins.back.utils;

import com.guguskins.back.common.exception.GlobalException;
import com.guguskins.back.entity.dto.LuckyDrawDto;
import com.guguskins.back.entity.dto.LuckyDrawEggDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @date 2021/5/10 10:19
 */
@Slf4j
public class CommonUtil {

    private static final String salt = "guguskins";

    /**
     * 生成验证码
     */
    public static String generateNumberCode() {
        return String.valueOf(new Random().nextInt(999999));
    }

    /**
     * 校验参数
     */
    public static void checkParams(Object... params) {
        if (!ObjectUtils.isEmpty(params)) {
            for (Object param : params) {
                if (ObjectUtils.isEmpty(param)) {
                    log.error("参数为空");
                    throw new GlobalException();
                }
            }
        }
    }

    public static int generateInt() {
        int i = UUID.randomUUID().hashCode();
        if (i < 0) {
            i = -i;
        }
        return i;
    }

    /**
     * 抽奖
     */
    public static Long luckyDraw(List<LuckyDrawDto> luckyDrawDtos) {
        //创建区间数组
        /*List<BigDecimal> interval = new ArrayList<>();
        BigDecimal start = new BigDecimal(BigInteger.ZERO);
        interval.add(start);
        for (LuckyDrawDto luckyDrawDTO : luckyDrawDtos) {
            start = start.add(luckyDrawDTO.getProbability());
            interval.add(start);
        }*/
        //生成随机数
        BigDecimal random = BigDecimal.valueOf(Math.random() * (luckyDrawDtos.stream().map(LuckyDrawDto::getProbability).reduce(BigDecimal.ZERO, BigDecimal::add)).doubleValue());
        //比对区间
        BigDecimal start = new BigDecimal(BigInteger.ZERO);
        for (LuckyDrawDto luckyDrawDTO : luckyDrawDtos) {
            start = start.add(luckyDrawDTO.getProbability());
            if (random.compareTo(start) < 0) {
                return luckyDrawDTO.getId();
            }
        }
        return null;
    }



    public static Integer luckyDrawEgg(LuckyDrawEggDto luckyDrawEggDTO) {
        //创建区间数组
        /*List<BigDecimal> interval = new ArrayList<>();
        BigDecimal start = new BigDecimal(BigInteger.ZERO);
        interval.add(start);
        for (LuckyDrawDto luckyDrawDTO : luckyDrawDTOS) {
            start = start.add(luckyDrawDTO.getProbability());
            interval.add(start);
        }*/



        //生成随机数
        BigDecimal random = BigDecimal.valueOf(Math.random() * (100));
        //比对区间
        if (random.compareTo(luckyDrawEggDTO.getProbability()) < 0) {
            return (int) (Math.random() * (luckyDrawEggDTO.getMax()));
        }
        return 0;
    }

    /**
     * 抽奖
     */
    public static boolean luckyDrawByRate(BigDecimal rate) {
        //生成随机数
        BigDecimal random = BigDecimal.valueOf(Math.random() * (100));
        //比对区间
        return random.compareTo(rate) < 0;
    }

    /**
     * 生成随机字符串
     */
    public static String generateRandomStr() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * base64编码
     */
    public static String base64Encoder(String str) {
        str = salt + str + salt;
        return new String(Base64.getEncoder().encode(str.getBytes(StandardCharsets.UTF_8)));
    }

    /**
     * base64解码
     */
    public static String base64Decoder(String str) {
        String key = new String(Base64.getDecoder().decode(str.getBytes(StandardCharsets.UTF_8)));
        return key.replaceAll(salt, "");
    }

    /**
     * 使用流遍历操作
     */
    public static <T> List<List<T>> partition(final List<T> list, final int size) {
        int limit = (list.size() + size - 1) / size;
        List<List<T>> mglist = new ArrayList<>();
        Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> {
            mglist.add(list.stream().skip((long) i * size).limit(size).collect(Collectors.toList()));
        });
        return mglist;
    }

    public static String generateSn(String prefix) {
        return prefix + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + (int) ((Math.random() * 9 + 1) * 100000);
    }

    

    public static void main(String[] args) {
        /*double random = Math.random() * 100;
        System.err.println(random);*/

        List<LuckyDrawDto> luckyDrawDTOs = new ArrayList<>();
        LuckyDrawDto luckyDrawDTO1 = new LuckyDrawDto();
        luckyDrawDTO1.setId(1L);
        luckyDrawDTO1.setProbability(new BigDecimal(10));
        luckyDrawDTOs.add(luckyDrawDTO1);
        LuckyDrawDto luckyDrawDTO2 = new LuckyDrawDto();
        luckyDrawDTO2.setId(2L);
        luckyDrawDTO2.setProbability(new BigDecimal(20));
        luckyDrawDTOs.add(luckyDrawDTO2);
        LuckyDrawDto luckyDrawDTO3 = new LuckyDrawDto();
        luckyDrawDTO3.setId(3L);
        luckyDrawDTO3.setProbability(new BigDecimal(30));
        luckyDrawDTOs.add(luckyDrawDTO3);
        LuckyDrawDto luckyDrawDTO4 = new LuckyDrawDto();
        luckyDrawDTO4.setId(4L);
        luckyDrawDTO4.setProbability(new BigDecimal(39));
        luckyDrawDTOs.add(luckyDrawDTO4);

        LuckyDrawDto luckyDrawDTO5 = new LuckyDrawDto();
        luckyDrawDTO5.setId(5L);
        luckyDrawDTO5.setProbability(new BigDecimal(1));
        luckyDrawDTOs.add(luckyDrawDTO5);

        for (int i = 0; i < 100; i++) {
            if (luckyDraw(luckyDrawDTOs) == 5) {
                System.err.println(i);
            }
        }
//        System.err.println(generateRandomStr());

//        System.err.println(generateStrNoStriping());
    }
}
